//
//  ArrayDataSource.m
//  27-ArrayDataSource
//
//  Created by mafeng on 16/8/19.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ArrayDataSource.h"

@interface ArrayDataSource ()
/**
 *  声明初始化参数中的属性
 */
@property (nonatomic, copy) NSString *reuse;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, copy) configCell configCell;
@end
@implementation ArrayDataSource

- (instancetype)initWithTableCellReuse:(NSString *)reuse arrayDataSource:(NSArray *)array configCell:(configCell)cofigCell {
    self = [super init];
    if (self) {
        // 属性赋值
        _reuse = reuse;
        _dataSource = array;
        _configCell = cofigCell;
    }
    return self;
}

+ (instancetype)arrayDataSourceWithTableCellReuse:(NSString *)reuse arrayDataSource:(NSArray *)array configCell:(configCell)cofigCell {
    return [[ArrayDataSource alloc] initWithTableCellReuse:reuse arrayDataSource:array configCell:(void (^)(UITableViewCell *cell, id model))cofigCell];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_reuse];
    // 根据数据源和cell调用block
    id model = _dataSource[indexPath.row];
    self.configCell(cell, model);
    return cell;
}


@end
